package com.osmino.contacts.provider;

import java.util.ArrayList;
import java.util.List;
import android.content.Intent;
import android.net.Uri;
import com.osmino.contacts.entity.Contact;
import com.osmino.contacts.entity.Email;
import com.osmino.contacts.entity.Phone;

public final class SendMessageUtils {

	public static final Intent emailListContacts(List<Contact> listContacts) {
		Intent call = new Intent(Intent.ACTION_SEND);
		call.setType("text/plain");
		List<String> recipientsList = new ArrayList<String>();
		for (Contact contact : listContacts) {
			for (Email email : contact.getEmails()) {
				recipientsList.add(email.address);
			}
		}
		String[] recipientsArray = new String[recipientsList.size()];
		recipientsList.toArray(recipientsArray);
		call.putExtra(Intent.EXTRA_EMAIL, recipientsArray);
		return call;
	}

	public static final Intent emailContact(Contact contact) {
		List<Contact> listContacts = new ArrayList<Contact>();
		listContacts.add(contact);
		return emailListContacts(listContacts);
	}

	public static final Intent emailIntent(Email email) {
		Intent call = new Intent(Intent.ACTION_SEND);
		call.setType("text/plain");
		call.putExtra(Intent.EXTRA_EMAIL, new String[] { email.address });
		return call;
	}

	public static final Intent smsListContacts(List<Contact> listContacts) {
		StringBuilder uriStringBuilder = new StringBuilder("smsto:");
		boolean atLeastOneAdded = false;
		for (Contact contact : listContacts) {
			for (Phone phone : contact.getPhones()) {
				if (atLeastOneAdded) {
					uriStringBuilder.append(";");
				}
				uriStringBuilder.append(phone.number);
				atLeastOneAdded = true;
			}
		}
		Intent call = null;
		if (atLeastOneAdded) {
			call = new Intent(Intent.ACTION_SENDTO);
			call.setData(Uri.parse(uriStringBuilder.toString()));
		}
		return call;
	}

	public static final Intent smsContact(Contact contact) {
		List<Contact> listContacts = new ArrayList<Contact>();
		listContacts.add(contact);
		return smsListContacts(listContacts);
	}

	public static final Intent smsIntent(Phone phone) {
		StringBuilder uriStringBuilder = new StringBuilder("smsto:");
		uriStringBuilder.append(phone.number);
		Intent call = new Intent(Intent.ACTION_SENDTO);
		call.setData(Uri.parse(uriStringBuilder.toString()));
		return call;

	}

	public static final Intent sendContactBySms() {
		return null;
	}
}
